const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')

const typeDefs = gql`
  #课程类型
  type Course {
    cname: String
    score: Float
  }
  #学生类型
  type Student {
    id: ID
    sname: String
    age: Int
    scores(num: Float): [Course]
  }
  #查询类型
  type Query {
    hello: String
    stu(id: Int): Student
  }
`

const resolvers = {
  Student: {
    scores: (parent, args) => {
      return (
        parent.scores &&
        parent.scores.filter(item => {
          console.log(item.score)
          return item.score > args.num
        })
      )
    }
  },
  Query: {
    hello: () => 'Hello world!111',
    stu: (parent, args) => {
      if (args.id === 1) {
        return {
          id: 1,
          sname: '张三',
          scores: [
            {
              cname: '数学',
              score: 99.5
            },
            {
              cname: '语文',
              score: 90.5
            }
          ]
        }
      } else {
        return {
          id: 0,
          sname: '---',
          scores: null
        }
      }
    }
  }
}

const server = new ApolloServer({ typeDefs, resolvers })

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 }, () => console.log('Now browse to http://localhost:4000' + server.graphqlPath))
/**
query param($id:Int,$num:Float){
  hello
 stu(id:$id){
  id
  sname
  scores(num:$num){
    cname
    score
  }
}
}

{
  "id": 2,
  "num": 92
}
 */
